package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author Hu
 * @create 2021-09-29 16:54
 * @Description
 */


@Service
@Slf4j
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    ProductConverter productConverter;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = productConverter.itemToProductDetailDto(item);
            //获取detail
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            productDetailDto.setDetail(itemDesc.getItemDesc());
            //获取Big和small
            String[] images = item.getImages();
            List<String> list = new ArrayList<>();
            for (String image : images) {
                list.add(image);
            }
            productDetailDto.setProductImageSmall(list);
            String imageBig = item.getImageBig();
            productDetailDto.setProductImageBig(imageBig);

            //参数设置结束
            response.setProductDetailDto(productDetailDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();

        //request.check 低价不能大于高价需要验证 //TODO: -1价格降序 1讲个升序

        try {
            request.requestCheck();
            Example example = new Example(Item.class);
            if ("-1".equals(request.getSort())) {
                example.setOrderByClause("price desc");
            }
            if ("1".equals(request.getSort())) {
                example.setOrderByClause("price asc");
            }
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("status", 1);
            if (request.getPriceGt() != null) {
                criteria.andGreaterThanOrEqualTo("price", request.getPriceGt());
            }
            if (request.getPriceLte() != null) {
                criteria.andLessThanOrEqualTo("price", request.getPriceLte());
            }
            if (request.getCid() != null) {
                criteria.andEqualTo("cid", request.getCid());
            }

            //分页
            PageHelper.startPage(request.getPage(), request.getSize());
            List<Item> items = itemMapper.selectByExample(example);
            PageInfo<Item> productDtoPageInfo = new PageInfo<>(items);


            List<ProductDto> productDtos = productConverter.items2Dto(items);

            response.setTotal(productDtoPageInfo.getTotal());
            response.setProductDtoList(productDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            log.info("com.mall.shopping.services.IProductServiceImpl.getAllProduct success query rows"+productDtoPageInfo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            log.info("com.mall.shopping.services.IProductServiceImpl.getAllProduct occur error:"+e);

        }

        return response;
    }


    //查找panelId为6的商品为热门商品类
    @Override
    public RecommendResponse getRecommendGoods() {
//        无参数传入 仅考虑查表抛出异常
//        Example example = new Example(PanelContent.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("panelId", 6);
        RecommendResponse response = new RecommendResponse();
        try {
            List<PanelContentItem> panelContents = panelContentMapper.selectPanelContentAndProductWithPanelId(6);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContents);
            Panel panel = panelMapper.selectByPrimaryKey(6);
            //手动转出PanelDto
            PanelDto panelDto = new PanelDto();
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDto.setId(panel.getId());
            panelDto.setName(panel.getName());
            panelDto.setSortOrder(panel.getSortOrder());
            panelDto.setPosition(panel.getPosition());
            panelDto.setLimitNum(panel.getLimitNum());
            panelDto.setStatus(panel.getStatus());
            panelDto.setRemark(panel.getRemark());

            //建个set的方法
            HashSet<PanelDto> panelDtos = new HashSet<>();
            panelDtos.add(panelDto);
            response.setPanelContentItemDtos(panelDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            log.info("com.mall.shopping.services.IProductServiceImpl.getRecommendGoods success: ");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("com.mall.shopping.services.IProductServiceImpl.getRecommendGoods occur Exception:" + e);

        }
        return response;
    }
}
